package com.itheima.youdianda.controller;



import com.itheima.youdianda.common.R;


import com.itheima.youdianda.dto.UserRole;
import com.itheima.youdianda.entity.YddUser;
import com.itheima.youdianda.service.SysUserService;
import com.itheima.youdianda.service.YddUserService;
import com.itheima.youdianda.util.MyPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_user")
@Slf4j
public class SysUserController {
    @Autowired
    private SysUserService iSysUserService;

    @GetMapping("/list")
    public R listByPage(String login_name, String status, String phone, @RequestParam(defaultValue = "1") String page, @RequestParam(defaultValue = "10") String limit){
        log.info("/sys_user/list,login_name:{},status:{},phone:{},page:{},limit:{}",login_name,status,phone,page,limit);
        MyPage<UserRole> userRolePage = iSysUserService.listByPage(login_name, status, phone, page,limit);
        return R.success(userRolePage);
    }

    @PostMapping("/update")
    public R update(@RequestBody UserRole userRole){
        log.info("/sys_user/update,userRole:{}",userRole);
        iSysUserService.updateByIdWithRoleName(userRole);
        return R.success("修改成功!");
    }

    @GetMapping("/rowInfo")
    public R rowInfo(@RequestParam String id){
        log.info("/sys_user/rowInfo,id:{}",id);
        UserRole user = iSysUserService.getByIdWithRoleName(id);
        return R.success(user);
    }

    @PostMapping("/add")
    public R add(@RequestBody UserRole userRole){
        log.info("/sys_user/add,userRole:{}",userRole);
        userRole.setCreate_date(LocalDateTime.now());
        iSysUserService.add(userRole);
        return R.success("添加成功!");
    }

    @GetMapping("/del")
    public R del(@RequestParam String id){
        log.info("/sys_user/del,id:{}",id);
        iSysUserService.removeById(id);
        return R.success("删除成功!");
    }
}
